package net.wanho.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import net.wanho.dto.LoginLogDTO;
import net.wanho.dto.OperaLogDTO;
import net.wanho.mapper.LogMapper;
import net.wanho.mapper.OperMapper;
import net.wanho.po.LoginLog;
import net.wanho.po.OperLog;
import net.wanho.service.LogService;
import net.wanho.vo.PageInfo;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

/**
 * 这个类是：
 *
 * @author: MENG HU WANG
 * @date: 2025/2/7 16:35
 * @version: 1.0
 */
@Service
@RequiredArgsConstructor
public class LogServiceImpl implements LogService {

    private final LogMapper logMapper;
    private final OperMapper operMapper;

    @Override
    public PageInfo<LoginLog> getLoginLogPageList(int pageNum, int pageSize, LoginLogDTO loginLogDTO) {
        Page<LoginLog> page = new Page<>(pageNum, pageSize);
        QueryWrapper<LoginLog> wrapper = new QueryWrapper<>();
        wrapper.eq(!ObjectUtils.isEmpty(loginLogDTO.getUserName()), "user_name", loginLogDTO.getUserName());
        wrapper.eq(!ObjectUtils.isEmpty(loginLogDTO.getIpaddr()), "ipaddr", loginLogDTO.getIpaddr());
        wrapper.eq(!ObjectUtils.isEmpty(loginLogDTO.getLoginLocation()), "login_location", loginLogDTO.getLoginLocation());
        wrapper.eq(!ObjectUtils.isEmpty(loginLogDTO.getStatus()), "status", loginLogDTO.getStatus());
        wrapper.ge(!ObjectUtils.isEmpty(loginLogDTO.getStartTime()), "login_time", loginLogDTO.getStartTime());
        wrapper.le(!ObjectUtils.isEmpty(loginLogDTO.getEndTime()), "login_time", loginLogDTO.getEndTime());
        logMapper.selectPage(page, wrapper);

        PageInfo<LoginLog> pageInfo = new PageInfo<>();
        pageInfo.setPageNum(page.getCurrent());
        pageInfo.setPageSize(page.getSize());
        pageInfo.setTotalNum(page.getTotal());
        pageInfo.setPageCount(page.getPages());
        pageInfo.setLimitIndex(page.offset());
        pageInfo.setResultList(page.getRecords());
        return pageInfo;
    }

    @Override
    public PageInfo<OperLog> getOperaLogPageList(int pageNum, int pageSize, OperaLogDTO operaLogDTO) {
        Page<OperLog> page = new Page<>(pageNum, pageSize);
        QueryWrapper<OperLog> wrapper = new QueryWrapper<>();
        wrapper.eq(!ObjectUtils.isEmpty(operaLogDTO.getOperName()), "oper_name", operaLogDTO.getOperName());
        wrapper.eq(!ObjectUtils.isEmpty(operaLogDTO.getBusinessType()), "business_type", operaLogDTO.getBusinessType());
        wrapper.eq(!ObjectUtils.isEmpty(operaLogDTO.getStatus()), "status", operaLogDTO.getStatus());
        wrapper.ge(!ObjectUtils.isEmpty(operaLogDTO.getStartTime()), "oper_time", operaLogDTO.getStartTime());
        wrapper.le(!ObjectUtils.isEmpty(operaLogDTO.getEndTime()), "oper_time", operaLogDTO.getEndTime());
        operMapper.selectPage(page, wrapper);

        PageInfo<OperLog> pageInfo = new PageInfo<>();
        pageInfo.setPageNum(page.getCurrent());
        pageInfo.setPageSize(page.getSize());
        pageInfo.setTotalNum(page.getTotal());
        pageInfo.setPageCount(page.getPages());
        pageInfo.setLimitIndex(page.offset());
        pageInfo.setResultList(page.getRecords());
        return pageInfo;
    }

    @Override
    @Async
    public void add(LoginLog loginLog) {
        logMapper.insert(loginLog);
    }
}
